/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tictactoe;

import engine.Singleton;
import figure.Player;
import java.io.IOException;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import org.json.simple.parser.ParseException;
import server.ConnectClient;

/**
 *
 * @author User
 */
public class UserComman {
  private static JScrollPane ScrollList;
  private static DefaultListModel listModel;
  private static JList list;
  private static ConnectClient cli;
  private static javax.swing.JPanel FieldPannel;

   
  public UserComman(JScrollPane ScrollList, javax.swing.JPanel FieldPannel){
  this.ScrollList = ScrollList;
  this.FieldPannel = FieldPannel;
}
  
  
  public static void play(boolean figure, boolean step)
  {
    cli.fig = figure;
    cli.step = step;
    for (int j=0; j<3; j++)
    {
      for (int i = 0; i < 3; i++) {
        SmallField field = new SmallField(j, i, figure, step);
        cli.matrix[j][i] = field;
        field.setLocation(100 * i, 100*j);
        FieldPannel.add(field);
      }
    }
    FieldPannel.repaint();
  }
  
  
  public static void connection(String port, String login, String ip, VisualClien visual, JPanel FieldPannel) throws IOException, ParseException
  {
    int porter = Integer.parseInt(port);
    cli = new ConnectClient(porter, login, ip);
    cli.FieldPannel = FieldPannel;
    visual.setClient(cli);
  }
  
  public static void disconnect(){
    //Singleton.outClient = "{\"Type\": \"End\"}";
     cli.disconnect();
  }
  
  public static void addUserToList(){
     listModel = new DefaultListModel();
     list = new JList(listModel);

    List<String> li = Singleton.Clientusers;
    for (String f : li){
     // ScrollList.add(new Player(f));
       listModel.addElement(f);
    }
    //ScrollList.repaint();
    ScrollList.setViewportView(list);
  }
  
  public static void setPlaer(javax.swing.JPanel FieldPannel)
  {
    int index = list.getSelectedIndex();    
    String player = (String) listModel.getElementAt(index);
    System.out.println(listModel.getElementAt(index));
    cli.setPlaer(player);
    //cli.FieldPannel = FieldPannel;
  }
  
  public static void refuse(String name)
  {
    String send = "{\"Type\":\"Refuse\", \"Name\":\"" + name + "\"}";
    cli.refuse(send);
  }
  
  public static void startGame()
  {
    String send = "{\"Type\":\"StartGame\", \"YouLogin\":\"" + cli.getLogin() + "\", \"PartnerLogin\":\"" + cli.getPartnerName() + "\"}";
    cli.refuse(send);
  }
  
  public static void refresh()
  {
    String send = "{\"Type\":\"Refresh\"}";
    cli.refuse(send);
  }
  
  public static void waiter()
  {
    String send = "{\"Type\":\"Wait\"}";
    cli.refuse(send);
  }
  
  public static void step(int row, int col)
  {
   // String send = "{\"Type\":\"Step\", \"Row\":\"" + row + "\", \"Col\":\"" + col + "\"}";
    String send = "{\"Type\":\"Step\", \"Row\":\"" + row + "\", \"Col\":\"" + col + "\", \"Name\":\"" + cli.getLogin() + "\"}";
    cli.refuse(send);
    block();
    cli.steps[row][col] = true;
  }
  
  private static void block()
  {
    SmallField[][] matrix = cli.matrix;
    for (int j=0; j<3; j++)
    {
      for (int i = 0; i < 3; i++) {
        SmallField now = matrix[j][i];
        now.setStep(false);
      }
    }
  }
  
   public static void disblock() {
    for (int j=0; j<3; j++)
    {
      for (int i = 0; i < 3; i++) {
        SmallField now = cli.matrix[j][i];
        if (!cli.steps[j][i]){
          now.setStep(true);
        }
      }
    }
  }
   
   public static void clearField()
   {
     cli.FieldPannel.removeAll();
    cli.FieldPannel.repaint();
    for (int j=0; j<3; j++)
    {
      for (int i = 0; i < 3; i++) {
        cli.matrix[j][i] = null;
        cli.steps[j][i] = false;
      }
    }
   }
  
}
